package com.sd.repay.transfer.impl;

import com.sd.repay.dao.BaseDao;
import com.sd.repay.pojo.TransferChannelInfo;
import com.sd.repay.pojo.TransferOrder;
import com.sd.repay.pojo.TransferRoute;
import com.sd.repay.transfer.TransferService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author 666666
 */
@Transactional(propagation = Propagation.NOT_SUPPORTED)
@Service
public class TransferServiceImpl implements TransferService {

    @Autowired
    private BaseDao dao;

    @Override
    public Long saveTransferOrder(TransferOrder transferOrder) throws SQLException, Exception {
        List<Object> condition = new ArrayList<>();
        String sql = "INSERT INTO t_transfer_order(order_type,source_order_no,trade_source,mer_type,mer_no,out_pay_bank,acq_mer_no,out_acc_no,in_acc_no,in_token,in_acc_name,";
        sql += "in_settle_bank_no,in_bank_no,in_bank_name,mobile_no,identity_type,identity_no,amount,fee, STATUS,summary,bak1,bak2) VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";

        condition.add(transferOrder.getOrderType());
        condition.add(transferOrder.getSourceOrderNo());
        condition.add(transferOrder.getTradeSource());
        condition.add(transferOrder.getMerType());
        condition.add(transferOrder.getMerNo());
        condition.add(transferOrder.getOutPayBank());
        condition.add(transferOrder.getAcqMerNo());
        condition.add(transferOrder.getOutAccNo());
        condition.add(transferOrder.getInAccNo());
        condition.add(transferOrder.getInToken());
        condition.add(transferOrder.getInAccName());
        condition.add(transferOrder.getInSettleBankNo());
        condition.add(transferOrder.getInBankNo());
        condition.add(transferOrder.getInBankName());
        condition.add(transferOrder.getMobileNo());
        condition.add(transferOrder.getIdentityType());
        condition.add(transferOrder.getIdentityNo());
        condition.add(transferOrder.getAmount());
        condition.add(transferOrder.getFee());
        condition.add("0");
        condition.add(transferOrder.getSummary());
        condition.add(transferOrder.getBak1());
        condition.add(transferOrder.getBak2());

        return dao.getSaveObjId(sql, condition.toArray());
    }

    @Override
    public List<TransferOrder> getTransferOrderListByTime(String startTime, String endTime, String... status) {
        String sql = "select * from t_transfer_order where create_time >= ? and create_time <= ?";
        List<String> condition = new ArrayList<>();

        condition.add(startTime);
        condition.add(endTime);

        if (status != null && status.length > 0) {
            sql += " and status in(";
            for (int i = 0; i < status.length; i++) {
                sql += "?";
                if (i != (status.length - 1)) {
                    sql += ",";
                }
                condition.add(status[i]);
            }
            sql += ")";
        }

        return dao.findList(TransferOrder.class, sql, condition.toArray());
    }

    @Override
    public TransferOrder getTransferOrderById(String orderId, String status) {
        ArrayList<String> condition = new ArrayList<>();
        condition.add(orderId);
        String sql = "select * from t_transfer_order where id = ?";
        if (StringUtils.isNoneBlank(status)) {
            sql += " and status = ?";
            condition.add(status);
        }
        return dao.findFirst(TransferOrder.class, sql, condition.toArray());
    }

    @Override
    public TransferOrder getTransferOrder(String orderType, String sourceOrderNo, String status) {
        ArrayList<String> condition = new ArrayList<>();
        condition.add(orderType);
        condition.add(sourceOrderNo);
        String sql = "select * from t_transfer_order where order_type = ? and source_order_no = ?";
        if (StringUtils.isNoneBlank(status)) {
            sql += " and status = ?";
            condition.add(status);
        }
        return dao.findFirst(TransferOrder.class, sql, condition.toArray());
    }

    @Override
    public List<TransferChannelInfo> getTransferChannelInfoList() {
        String sql = "select * from t_transfer_channel where bank_status = '1'  order by create_time desc";
        return dao.findList(TransferChannelInfo.class, sql);
    }

    @Override
    public TransferChannelInfo getTransferChannelInfo(String payBankCode) {
        String sql = "select * from t_transfer_channel where bank_code = ? and bank_status = '1'  order by create_time desc";
        return dao.findFirst(TransferChannelInfo.class, sql, payBankCode);
    }

    @Override
    public TransferChannelInfo getTransferChannelInfo(String payBankCode, String outAccNo) {
        String sql = "select * from t_transfer_channel where bank_code = ? and bank_merchant_no = ? and bank_status = '1'  order by create_time desc";
        return dao.findFirst(TransferChannelInfo.class, sql, payBankCode, outAccNo);
    }

    @Override
    public TransferRoute getTransferRoute(String tradeSource) {
        String sql = "select * from t_transfer_route where trade_source = ? and status = '1'";
        return dao.findFirst(TransferRoute.class, sql, tradeSource);
    }

    @Override
    public int modifySubStaTransfer(TransferOrder transferOrder) {
        String sql = "update t_transfer_order set status=?, sub_result_code=?, sub_result_msg=?, sub_order_no=?, acq_order_no=?, summary=?,bak1=?,bak2=?,pay_time=now(),last_update_time=now() where id = ? and status = 0";

        List<Object> condition = new ArrayList<>();
        condition.add(transferOrder.getStatus());
        condition.add(transferOrder.getSubResultCode());
        condition.add(transferOrder.getSubResultMsg());
        condition.add(transferOrder.getSubOrderNo());
        condition.add(transferOrder.getAcqOrderNo());
        condition.add(transferOrder.getSummary());
        condition.add(transferOrder.getBak1());
        condition.add(transferOrder.getBak2());
        condition.add(transferOrder.getId());
        return dao.execute(sql, condition.toArray());
    }

    @Override
    public int modifySysStaTransfer(TransferOrder transferOrder) {
        String sql = "update t_transfer_order set status=?,result_code=?,result_msg=?,acq_order_no=?,bak1=?,bak2=?,last_update_time=now() where id = ? and status in('1','6')";

        List<Object> condition = new ArrayList<>();
        condition.add(transferOrder.getStatus());
        condition.add(transferOrder.getResultCode());
        condition.add(transferOrder.getResultMsg());
        condition.add(transferOrder.getAcqOrderNo());
        condition.add(transferOrder.getBak1());
        condition.add(transferOrder.getBak2());
        condition.add(transferOrder.getId());
        return dao.execute(sql, condition.toArray());
    }


}
